# -*- coding: utf-8 -*-

from Gomuku import *
import requests
import datetime
import time

# url = 'http://192.168.1.105:5000/'
url = 'http://127.0.0.1:5000/'

board_dict = dict()
time_delay = 0.1


def getBoardDictOnline():
    global board_dict

    # '/board' GET 得到board_dict
    r1 = requests.get(url + 'board')
    board_dict = eval(r1.text)
    # print(board_dict)
    pass


def postBoardDictOnline(new_board, new_runner):
    global board_dict

    board_dict['board'] = new_board
    board_dict['runner'] = new_runner
    # print(board_dict)
    # '/board' POST 提交board_dict
    r3 = requests.post(url + 'board', data=board_dict)
    print(str(datetime.datetime.now().strftime('%H:%M:%S')) + r3.text)
    pass


def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    who_am_i = WHITE_CHESSMAN
    pygame.display.set_caption('欢乐五子棋' + who_am_i.name)

    font1 = pygame.font.SysFont('SimHei', 32)  # 黑体32
    font2 = pygame.font.SysFont('SimHei', 72)  # 黑体72
    f1width, f1height = font2.size('黑子获胜')
    f2width, f2height = font1.size('Press SPACE to continue...')

    board = Board(LINES)
    cur_runner = BLACK_CHESSMAN
    winner = None

    while True:
        for event in pygame.event.get():
            # “退出”事件
            if event.type == QUIT:
                postBoardDictOnline([[0] * LINES for _ in range(LINES)], BLACK_CHESSMAN.name)
                sys.exit()
            elif event.type == KEYDOWN:
                # 空格按下
                if event.key == K_SPACE:
                    if winner is not None:
                        winner = None
                        cur_runner = BLACK_CHESSMAN
                        board = Board(LINES)
                        postBoardDictOnline(board.getBoard(), cur_runner.name)
                        pass
                    pass
                pass
            elif event.type == MOUSEBUTTONDOWN:
                if winner is None:
                    pressed_array = pygame.mouse.get_pressed(3)
                    # 左键按下
                    if pressed_array[0]:
                        getBoardDictOnline()
                        runner = board_dict['runner']
                        if runner == 'black':
                            cur_runner = BLACK_CHESSMAN
                        elif runner == 'white':
                            cur_runner = WHITE_CHESSMAN
                            pass

                        if cur_runner.name == who_am_i.name:
                            mouse_pos = pygame.mouse.get_pos()
                            click_point = getClickPoint(mouse_pos)
                            if click_point is not None:
                                if board.canDrop(click_point):
                                    # 下子
                                    winner = board.drop(cur_runner, click_point)
                                    if winner is None:
                                        cur_runner = getNextRunner(cur_runner)
                                        pass

                                    # 更新棋盘
                                    postBoardDictOnline(board.getBoard(), cur_runner.name)
                                    pass
                                pass
                            else:
                                print('超过棋盘范围...')
                                pass
                            pass
                        pass
                    pass
                pass
            pass

        # 从服务器更新棋盘
        getBoardDictOnline()
        new_board = board_dict['board']
        cur_runner = board_dict['runner']
        board.setBoard(new_board)

        # 画棋盘
        drawBoard(screen)
        # 画棋子
        drawStones(board, screen)

        # 结算界面
        if winner:
            print_text(screen, font2, (SCREEN_WIDTH - f1width)//2, (SCREEN_HEIGHT - f1height)//2 - f2height,
                       winner.name + '获胜', RED_COLOR)
            print_text(screen, font1, (SCREEN_WIDTH - f2width)//2, (SCREEN_HEIGHT - f1height)//2 + 2*f2height,
                       "Press SPACE to continue...", RED_COLOR)

        pygame.display.flip()
        time.sleep(time_delay)
        pass
    pass


if __name__ == '__main__':
    main()
    pass
